package com.hqbzl.c1.cass;

import org.apache.log4j.chainsaw.Main;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;

public class TestAtomic {
    public static void main(String[] args) {
        final AtomicInteger atomicInteger = new AtomicInteger(100);
        //final int i = atomicInteger.incrementAndGet();
//        atomicInteger.compareAndSet()
        //final int i1 = atomicInteger.compareAndExchange(100, 90);
        //System.out.println("i1 = " + i1);
        //System.out.println("atomicInteger.get() = " + atomicInteger.get());
        //System.out.println("atomicInteger.compareAndSet(100, 10) = " + atomicInteger.compareAndSet(10, 90));
        //System.out.println("atomicInteger.compareAndSet(100, 90) = " + atomicInteger.compareAndSet(100, 90));
        //System.out.println("atomicInteger.get() = " + atomicInteger.get());
        //final int andIncrement = atomicInteger.getAndIncrement(); ++i
        //final int i = atomicInteger.incrementAndGet(); i ++
//        test1();
//        test2();
        test3();
    }

    public static void test1() {
        final AtomicIntegerArray array = new AtomicIntegerArray(10);
        final AtomicInteger atomicInteger = new AtomicInteger(0);
        System.out.println("atomicInteger.get() = " + atomicInteger.get());
        System.out.println("atomicInteger.getAndIncrement() = " + atomicInteger.getAndIncrement());// i++
        System.out.println("atomicInteger.incrementAndGet() = " + atomicInteger.incrementAndGet()); // ++i
        System.out.println("atomicInteger.get() = " + atomicInteger.get());
    }

    public static void test2() {
        final AtomicInteger atomicInteger = new AtomicInteger(100);
        System.out.println("atomicInteger = " + atomicInteger);
        System.out.println("atomicInteger.getAndAdd(10) = " + atomicInteger.getAndAdd(10)); // 先获取在+
        System.out.println("atomicInteger1 = " + atomicInteger);
        System.out.println("atomicInteger.addAndGet(3) = " + atomicInteger.addAndGet(3)); //  +后在返回、
        System.out.println("atomicInteger2 = " + atomicInteger);
    }

    public static void test3() {
        final AtomicInteger atomicInteger = new AtomicInteger(100);
        final int i = atomicInteger.updateAndGet(x -> x * 10); // 乘法运算 先得新在返回
        System.out.println("i = " + i);
        System.out.println("atomicInteger.get() 1= " + atomicInteger.get());
        final int andUpdate = atomicInteger.getAndUpdate(x -> x / 10);
        System.out.println("atomicInteger.get() 2= " + atomicInteger.get());
    }
}
